package io.daocloud.userservice.controller;

import com.google.common.util.concurrent.RateLimiter;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * Author: Grissom date: 2020/7/9 8:24 下午
 */
@RestController
public class UserController {
	private static final RateLimiter rateLimiter = RateLimiter.create(100.0); // 设置每秒最多处理100个请求
	@PostMapping("/user")
//	@GetMapping("/user")
	public String  user() {
//		System.out.printf("111111111111111");
		if(!rateLimiter.tryAcquire()){
			return "Too many requests";
		}
		return "hello world";
	}

	// 限流 fallback 方法
//	public String userFallback() {
//		return "Too many requests";
//	}
	//	@HystrixCommand(commandProperties = {
//			@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "100"),
//			@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "100"),
//			@HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1000")
//	},fallbackMethod = "userFallback")
}
